Skip to content

tutorial/handling null values in protobuf and avro #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 16 commits into
base: master
Choose a base branch
from

Conversation

Cerchie
Copy link
Contributor

@Cerchie Cerchie commented Feb 16, 2024

need to figure out a smooth way to handle testing and a way to teach the reader about what happens when null values are handled/not. tagging @davetroiano

@Cerchie Cerchie requested a review from a team as a code owner February 16, 2024 16:09
Copy link
Contributor

@davetroiano davetroiano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LG so far @Cerchie!

.setTotalCost(random.nextDouble() * random.nextInt(100));
```

In this case, you'll receive a NullPointer error.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whats going to come next? the field being oneof where one is a boolean to signify null?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It has to be a protobuf schema addition... if I were to show it I think it'd be a oneof, but also there's a google wrapper option. unfortunately because of the way the API is defined with protocol buffers and kafka there are two methods with the same name and java chokes on the protocol buffer... not sure how to fix here

Copy link
Contributor

@davetroiano davetroiano Feb 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does optional do the trick? i.e. does everything just work if you do that? I'm looking at the guidance here:

If not using optional would add complexity or ambiguity, then use optional primitive fields. Wrapper types must not be used going forward.

which makes me think we might want to avoid the wrapper pattern... I'm not sure why this is the guidance, e.g., will wrapper types eventually be deprecated?

I'm thinking that if optional works, go with that, and we'd also want to update this docs section to recommend optional rather than use a wrapper

wdyt?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so with this protobuf definition:

message Purchase {
  optional string item = 1;
  double total_cost = 2;
  string customer_id = 3;
}

you still end up with a NullPointerException ¯_(ツ)_/¯

but that's a good point about the guidance, I'll pass it on to docs.

@Cerchie Cerchie requested a review from davetroiano February 29, 2024 21:07
# How to allow `null` field values in Avro and Protobuf
Let's say you're using an Avro or Protobuf schema, and sometimes you want to set a field named `item` to null. Say it's a pipeline that takes both donations and purchases to be filtered later, and the donations are processed as purchases with null items. How to adjust the schema to allow for a null value?

Avro natively supports null fields with the 'null' type. In the above example, in order to make the `item` field nullable, you can allow the type to be "string" or "null" in the following manner:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Avro natively supports null fields with the 'null' type. In the above example, in order to make the `item` field nullable, you can allow the type to be "string" or "null" in the following manner:
Avro natively supports null fields with the `null` type. In the above example, in order to make the `item` field nullable, you can allow the type to be "string" or "null" in the following manner:

From the top-level directory:

```
./gradlew clean :handling-null-values-in-avro-and-protobuf:kafka:test --info
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this didn't work for me on a fresh clone:

* What went wrong:
A problem occurred evaluating project ':handling-null-values-in-avro-and-protobuf:kafka'.
> /Users/dtroiano/Desktop/tutorials/handling-null-values-in-avro-and-protobuf/kafka/src/main/resources/confluent.properties (No such file or directory)

.setTotalCost(random.nextDouble() * random.nextInt(100));
```

In this case, you'll receive a NullPointer error. You can allow null values to be explicitly set with a [protocol wrapper type](https://protobuf.dev/reference/protobuf/google.protobuf/https://protobuf.dev/reference/protobuf/google.protobuf/).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
In this case, you'll receive a NullPointer error. You can allow null values to be explicitly set with a [protocol wrapper type](https://protobuf.dev/reference/protobuf/google.protobuf/https://protobuf.dev/reference/protobuf/google.protobuf/).
In this case, you'll receive a NullPointer error. You can allow null values to be explicitly set with a [protocol wrapper type](https://protobuf.dev/reference/protobuf/google.protobuf/).

.setTotalCost(random.nextDouble() * random.nextInt(100));
```

In this case, you'll receive a NullPointer error. You can allow null values to be explicitly set with a [protocol wrapper type](https://protobuf.dev/reference/protobuf/google.protobuf/https://protobuf.dev/reference/protobuf/google.protobuf/).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you planning to have an example with wrapper? or leave it as an exercise to the reader

.setTotalCost(random.nextDouble() * random.nextInt(100));
```

In this case, you'll receive a NullPointer error. You can allow null values to be explicitly set with a [protocol wrapper type](https://protobuf.dev/reference/protobuf/google.protobuf/https://protobuf.dev/reference/protobuf/google.protobuf/).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggest adding a clean up step that directs the user to delete the env created for this

@Cerchie Cerchie changed the title 🚧 WIP: tutorial/handling null values in protobuf and avro tutorial/handling null values in protobuf and avro Apr 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants